package com.jmm.neditor.utils.template;

import com.jmm.neditor.utils.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ClassUtils;

/**
 * HtmlTemplateUtil class
 *
 * @author 蒋时华
 * @date 2018-5-9
 */
public class HtmlTemplateUtil {

    public static Integer WECHAT_TEMPLATE = 1;

    public static String path = "";

    public static String weChatTemplateHtml(String contextHtml,Integer templateType) throws Exception {

        TemplateInfo templateInfo = new TemplateInfo();

        String path = ClassUtils.getDefaultClassLoader().getResource("templates").getPath()+"/";
        if(StringUtils.isEmpty(path)){
            throw new RuntimeException("获取项目路径错误");
        }
        StringBuffer pathBuffer = new StringBuffer(path);

        StringBuffer pathBuffetTemp = new StringBuffer(pathBuffer.toString());
        String htmlContext = "";
        if(templateInfo.TEMPLATE_LIST.containsKey(templateType)){
            if(!StringUtils.isEmpty(templateInfo.TEMPLATE_LIST.get(templateType))){
                String readText = HTMLUtil.read(pathBuffetTemp.append(templateInfo.TEMPLATE_LIST.get(templateType)).toString());
                htmlContext = readText.replace(templateInfo.CONTEXT_FLAG,contextHtml);
            }else{
                htmlContext = contextHtml;
            }
        }else{
            throw new RuntimeException("无法识别该模板类型");
        }

        pathBuffer.append("/editorPage/");
        HTMLUtil.isFolderExist(pathBuffer.toString(),true);
        StringBuffer fileId = new StringBuffer(String.valueOf(IdUtil.getId()));
        String filePath = pathBuffer.append(fileId).append(".html").toString();
        HTMLUtil.write(filePath,false, htmlContext);

        return fileId.toString();

    }


}
